/**
 * @COPYRIGHT Shanghai RaxTone-Tech Co.,Ltd.
 * @Title: Authenticator.java  
 * @Description: TODO 
 * @author jiajing.mo	 
 * @date 2012-9-11
 * @version V1.0 
 * 
 * Modification History: 
 * @date 2012-9-11  |  jiajing.mo  |  Created 
 */
package com.morn.fanfou.authenticator;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.morn.fanfou.FanfouApplication;
import com.morn.fanfou.api.Fanfou;
import com.morn.fanfou.model.OAuthToken;
import com.morn.fanfou.util.Constants;

/**
 * Description: TODO.<br>
 * <p>
 * 
 * @author jiajing.mo
 * @version V1.0
 */
public class Authenticator extends AbstractAccountAuthenticator {

	// Authentication Service context
	private final Context mContext;

	private Fanfou fanfou;

	public static final String KEY_AUTH_LOGIN = "authLogin";

	/**
	 * @param context
	 */
	public Authenticator(Context context) {
		super(context);
		this.mContext = context;
		fanfou = ((FanfouApplication) context.getApplicationContext()).getFanfou();
	}

	@Override
	public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
		return null;
	}

	@Override
	public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType,
			String[] requiredFeatures, Bundle options) throws NetworkErrorException {
		final AccountManager accountManager = AccountManager.get(mContext);
		Account[] accounts = accountManager.getAccountsByType(accountType);
		Intent intent = null;
		final Bundle bundle = new Bundle();
		if (accounts != null && accounts.length == 0) {
			intent = new Intent(mContext, LoginActivity.class);
			intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
			intent.putExtra(KEY_AUTH_LOGIN, true);
		} else {
			intent = new Intent(mContext, SingleAccountNotifyActivity.class);
		}
		bundle.putParcelable(AccountManager.KEY_INTENT, intent);
		return bundle;
	}

	@Override
	public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options)
			throws NetworkErrorException {
		Log.i("morn", "-------confirmCredentials");
		return null;
	}

	@Override
	public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
			Bundle options) throws NetworkErrorException {
		Log.i("morn", "-------getAuthToken");
		if (!authTokenType.equals(Constants.TOKEN_TYPE)) {
			final Bundle result = new Bundle();
			result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
			return result;
		}

		final AccountManager accountManager = AccountManager.get(mContext);
		final String password = accountManager.getPassword(account);
		if (password != null) {
			try {
				final OAuthToken oAuthToken = fanfou.getOAuthToken(account.name, password);
				if (oAuthToken != null) {
					final Bundle result = new Bundle();
					result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
					result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
					result.putString(AccountManager.KEY_AUTHTOKEN, oAuthToken.getToken());
					accountManager.setUserData(account, Constants.KEY_TOKEN_SECRET, oAuthToken.getTokenSecret());
					return result;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		final Intent intent = new Intent(mContext, LoginActivity.class);
		intent.putExtra("username", account.name);
		intent.putExtra("authtokenType", authTokenType);
		intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
		final Bundle bundle = new Bundle();
		bundle.putParcelable(AccountManager.KEY_INTENT, intent);
		return bundle;
	}

	@Override
	public String getAuthTokenLabel(String authTokenType) {
		Log.i("morn", "----getAuthTokenLabel");
		return null;
	}

	@Override
	public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType,
			Bundle options) throws NetworkErrorException {
		Log.i("morn", "----updateCredentials");
		return null;
	}

	@Override
	public Bundle getAccountRemovalAllowed(AccountAuthenticatorResponse response, Account account)
			throws NetworkErrorException {
		final Bundle result = new Bundle();
		result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
		return result;
	}

	@Override
	public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features)
			throws NetworkErrorException {
		Log.i("morn", "----hasFeatures");
		return null;
	}

}
